/**
 * Copyright (c) 2015-2018, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.manage.service;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.kit.FileKit;

import cn.hg.jfsa.model.JfsaDict;
import cn.hg.jfsa.model.JfsaFile;

/**
 * @author Golly
 */
public class JfsaFileService {

	public static String[] audioExtsNames = {
		"mp3", "mav"
	};
	public static Long BYTE_K = 1024L;
	public static Long BYTE_10K = 10 * BYTE_K;
	public static Long BYTE_M = BYTE_K * BYTE_K;
	public static DecimalFormat df = new DecimalFormat("0.0");
	public static String[] docExtsNames = {
		"doc", "docx", "txt", "wps", "rtf", "pdf", ""
	};
	/**
	 * 文件类型：file：一般文件、image：图片、video：视频、audio：音频、zip：压缩包、doc：文档、folder：文件夹
	 */
	public static String[] imageExtsNames = {
		"jpg", "gif", "bmp", "png", "jpeg"
	};
	public static String[] videoExtsNames = {
		"mp4", "mov"
	};
	public static String[] zipExtsNames = {
		"zip", "rar", "7z"
	};
	private static String FILE_TYPE = "file_type";
	@Inject
	private JfsaDictService dictService;

	public List<JfsaFile> getFile (String path) {
		return this.getFiles(new File(path));
	}

	public List<JfsaFile> getFiles (File path) {
		List<JfsaFile> fileObjectList = new ArrayList<>();
		File[] files = path.listFiles();

		for (File f : files) {

			if (f.isDirectory()) {
				JfsaFile fo = new JfsaFile();
				fo.setType("folder");
				fo.setName(f.getName());
				fo.setFolder("");
				fo.setSize(String.valueOf(f.listFiles().length));
				fileObjectList.add(fo);
			}

		}

		for (File f : files) {

			if (!f.isDirectory()) {
				JfsaFile fo = new JfsaFile();
				fo.setType(this.getFileType(f.getName()));
				fo.setSize(this.getSizeName(f.length()));
				fo.setName(f.getName());
				fo.setExtName(FileKit.getFileExtension(f.getName()));
				fo.setFolder("");
				fo.setLength(f.length());
				fileObjectList.add(fo);
			}

		}

		return fileObjectList;
	}

	public String getFileType (String fileName) {
		String ext = FileKit.getFileExtension(fileName);
		String type = "file";
		List<JfsaDict> dictList = this.dictService.getByParentKey(JfsaFileService.FILE_TYPE, "on");

		for (JfsaDict dict : dictList) {
			String[] typeArray = dict.getValue().split(",");

			if (this.inArray(typeArray, ext)) {
				type = dict.getKey();
				break;
			}

		}

		return type;
	}

	public String getSizeName (Long size) {

		if (size < BYTE_10K) {
			return String.valueOf(size) + " B";
		} else if (size > BYTE_10K && size < BYTE_M) {
			return df.format(size / (double) BYTE_K) + " K";
		} else {
			return df.format(size / (double) BYTE_M) + " M";
		}

	}

	private boolean inArray (String[] arrs, String val) {

		for (String arr : arrs) {

			if (arr.equalsIgnoreCase(val)) {
				return true;
			}

		}

		return false;
	}

}
